package top.lbqaq.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "Servlet1", value = "/Servlet1")
public class Servlet1 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String s = req.getParameter("num");
        if (s == null) {
            resp.sendRedirect("1-2.jsp");
            return;
        }
        int num = Integer.parseInt("".equals(s) ? "0" : s);
        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter out = resp.getWriter();
        out.print(num);
        out.print("<br/>");
        out.print("正三角<br/>");
        for (int i = 0; i < num; i++) {
            for (int j = 0; j < num - i; j++) {
                out.print("&nbsp");
            }
            for (int k = 0; k <= i * 2; k++) {
                out.print("*");
            }
            out.println("</br>");
        }
        out.print("倒三角<br/>");
        for (int i = num; i > 0; i--) {
            for (int j = num - i; j > 0; j--) {
                out.print("&nbsp");
            }
            for (int k = (i * 2) - 1; k > 0; --k) {
                out.print("*");
            }
            out.println("</br>");
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
